package com.cgtong.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.cgtong.android.R;
import com.cgtong.android.activity.ImagePreviewActivity;
import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.widget.GlideApp;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by chunc on 2017/10/19.
 */

public class ProblemDetailPictureAdapter extends BaseAdapter {

    private List<ProblemBean.ResourceBean> mList;
    private Context mContext;
    private LayoutInflater mInflater;

    public ProblemDetailPictureAdapter(Context mContext, List<ProblemBean.ResourceBean> mList) {
        this.mList = mList;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public ProblemBean.ResourceBean getItem(int position) {
        if (position <= mList.size() - 1) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_detail_picture, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (getItem(position) != null) {
            GlideApp.with(mContext).load(getItem(position).getBigContent())
                    .placeholder(R.drawable.icon_default_loading)
                    .error(R.drawable.icon_default_error).into(holder.imageView);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImagePreviewActivity.start(mContext, mList, position);
                }
            });
        }
        return convertView;
    }


    class ViewHolder {

        @BindView(R.id.img_view)
        ImageView imageView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
